package com.ys.service.amazonorder.impl;

import java.util.Map;

import com.ys.dto.EditDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.bo.vo.R;
import com.ys.dto.AddDto;
import com.ys.entity.amazonorder.TbBaseinfoSppm;
import com.ys.mapper.amazonorder.TbBaseinfoSppmMapper;
import com.ys.mapper.business.DiySqlMapper;
import com.ys.service.amazonorder.TbBaseinfoSppmService;

@Service("tbBaseinfoSppmService")
public class TbBaseinfoSppmServiceImpl implements TbBaseinfoSppmService {
	
	@Autowired
	private TbBaseinfoSppmMapper tbBaseinfoSppmMapper;
	@Autowired
	private DiySqlMapper diySqlMapper;
	@Override
	public R add(Map<String, Object> params) {
		TbBaseinfoSppm entity = JSON.parseObject(JSON.toJSONString(params), TbBaseinfoSppm.class);

		String s = "^.{1,20}$";
		if(!entity.getNameCn().matches(s)){
			return R.error("商品品名（中)输入有误,请重新输入");
		}
		String m ="^[a-zA-Z\\s]*$";
		if(!entity.getNameEn().matches(m)){
			return R.error("商品品名（英）输入有误,请重新输入");
		}
		if (new TbBaseinfoSppm().setNameCn(entity.getNameCn()).count() > 0) {
			return R.error("该数据已存在");
		}
		if (new TbBaseinfoSppm().setNameCn(entity.getNameEn()).count() > 0) {
			return R.error("该数据已存在");
		}
		tbBaseinfoSppmMapper.insert(entity);
		return R.ok(AddDto.closeAndReload());
	}
	
	@Override
	public R edit(Map<String, Object> params) {
		


		TbBaseinfoSppm entity = JSON.parseObject(JSON.toJSONString(params), TbBaseinfoSppm.class);

		String s = "^.{1,20}$";
		if(!entity.getNameCn().matches(s)){
			return R.error("商品品名（中)输入有误,请重新输入");
		}
		String m ="^[a-zA-Z\\s]*$";
		if(!entity.getNameEn().matches(m)){
			return R.error("商品品名（英）输入有误,请重新输入");
		}

		tbBaseinfoSppmMapper.update(entity);
		return R.ok(EditDto.closeAndReload());
	}

	/**
	 * 删除
	 */
	@Override
	@Transactional
	public R deleteTbBaseinfoSppm(Long id) throws Exception{
		diySqlMapper.deleteTbBaseinfoSppm(id);
		return R.ok(EditDto.closeAndReload());
	
	}


}
